package m.member.order.util;

import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.member.manage.model.MemberInfo;
import m.member.manage.model.ServeGoods;
import m.member.order.model.MemberRecharge;
import m.member.order.model.OrderDetail;
import m.member.order.model.OrderInfo;
import m.member.order.service.MemberRechargeService;
import m.member.order.service.OrderDetailService;
import m.member.order.service.OrderInfoService;
import m.system.exception.MException;
import m.system.util.DateUtil;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class TestDataInsertUtil extends Service {
    private static Random random;
    private static List<MemberInfo> memberList;
    private static List<ServeGoods> goodsList;

    public static void main(String[] a) throws SQLException, MException {
        random=new Random();
        memberList= ModelQueryList.getModelList(MemberInfo.class,"*");
        goodsList=ModelQueryList.getModelList(ServeGoods.class,"*");
        List<AdminLogin> adminList = ModelQueryList.getModelList(AdminLogin.class, new String[]{"*"},null,
                QueryCondition.not(QueryCondition.eq("oid","1")));
        Date date= DateUtil.format("2023-01-01","yyyy-MM-dd");
        int sm=new Double((new Date().getTime()/1000d-date.getTime()/1000d)/60d/60d/24d).intValue();
        TestDataInsertUtil util=new TestDataInsertUtil();
        for(int i=0;i<sm;i++){
            date=DateUtil.getAddDate(date,1);
            if(random.nextInt(10)>5) {
                util.insertRecharge(
                    DateUtil.getAddDate(date, random.nextInt(5) - 5),
                    adminList.get(random.nextInt(adminList.size()))
                );
            }
            for(int o=0,n= random.nextInt(20)+1;o<n;o++){
                util.insertOrder(
                    DateUtil.getAddDate(date,random.nextInt(5)-5),
                    adminList.get(random.nextInt(adminList.size()))
                );
            }
        }

    }
    private static final String[] rechargeType=new String[]{"A","B","C","D"};
    private static final Double[] payAmount=new Double[]{100d,200d,300d,500d,1000d};
    private static final Double[] giveAmount=new Double[]{10d,30d,50d,100d,300d};
    public void insertRecharge(Date date, AdminLogin admin){
        MemberRecharge model=new MemberRecharge();
        model.setMember(memberList.get(random.nextInt(memberList.size())));
        model.setType(rechargeType[random.nextInt(rechargeType.length)]);
        int n=random.nextInt(payAmount.length);
        model.setPayAmount(payAmount[n]);
        model.setGiveAmount(giveAmount[n]);
        model.setGivePoint(payAmount[n].intValue());
        model.setInputDate(date);
        model.setServeAdmin(admin);
        try {
            getService(MemberRechargeService.class).save(model,admin,admin.getOrgGroup());
            getService(MemberRechargeService.class).confirm(model,admin,admin.getOrgGroup());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static final String[] payType=new String[]{"0","A","B","C","D"};
    public void insertOrder(Date date, AdminLogin admin){
        OrderInfo model=new OrderInfo();
        model.setMember(memberList.get(random.nextInt(memberList.size())));
        model.setPayType(payType[random.nextInt(payType.length)]);
        model.setOtherAmount(0d);
        model.setInputDate(date);
        model.setServeAdmin(admin);
        try{
            getService(OrderInfoService.class).save(model,admin,admin.getOrgGroup());
            for(int i=0,n= random.nextInt(4)+1;i<n;i++){
                addOrderInfo(model);
            }
            getService(OrderInfoService.class).submit(model,admin);
            getService(OrderInfoService.class).confirm(model,admin,admin.getOrgGroup());
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public void addOrderInfo(OrderInfo order) throws Exception {
        OrderDetail model=new OrderDetail();
        model.setOrder(order);
        model.setServe(goodsList.get(random.nextInt(goodsList.size())));
        model.setNum((double) (random.nextInt(3)+1));
        getService(OrderDetailService.class).save(model);
    }
}
